# -*- coding: utf-8 -*-
'''
Created on 2013-5-16

@author: dyq
'''
from Common.htmlHlep import Find
from Common.tool import tool
from Cms.plus import  page,user
from Models import  models
import  os,Cms,datetime
from django.http import HttpResponse

savefile='swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,gif,jpg,jpeg,png,bmp,swf,flv'

def UpFile(request):
        imgfile = request.FILES.get('imgFile','')
        fileId=tool.guid()
        fname=GetFile(imgfile,tool.guid())
        if(fname[0]):
            fp = open(fname[1], 'wb')  #读写打开这个要上传的文件
            for content in imgfile.chunks(): #写
                fp.write(content)
            fp.close()
            
            info=models.Sys_File(FileId=fileId,FileName=imgfile.name,
                            Extension=fname[2],
                            FilePath=fname[1],FileSize=imgfile.size,IsBind=0,
                            UpTime=datetime.datetime.now(),ModelId=request.POST.get('ModelId',''))
            info.save()
            return Find.Json({"error":0,"url":"/AdminMain/SysFile/Show/?Id="+fileId,"id":fileId})
        else:
            return Find.Json({"error":1,"message":"扩展名不合法"})

def GetFile(imgfile,fileId):
    files=imgfile.name.split(".")
    if len(files)==0:
        return False,'未填写扩展名'
    kz=files[len(files)-1]
    
    if kz not in savefile.split(","):
        return False,'禁止上传本扩展名的文件'
        
    fname = __GetPath__(fileId)
    return True,fname,kz
  
#给文件安排一个保存的位置 
def __GetPath__(fileId):
        upfiledir="./static/upfile/"+datetime.datetime.now().strftime('%Y/%m%H')
        if os.path.exists(os.path.abspath(upfiledir))==False:
            os.makedirs(os.path.abspath(upfiledir))
        
        return os.path.abspath(upfiledir+"/"+fileId)
    
#编辑文本文件
def EditTxt(all_the_text,TxtId):
        info=models.Sys_File.objects.get(FileId=TxtId)
        info.FileSize=len(all_the_text)
        info.UpTime=datetime.datetime.now()
        file_object = open(info.FilePath, 'w')
        file_object.writelines(all_the_text)
        file_object.close()
        return info

#保存文本文件
def SetTxt(all_the_text,ModelId=""):
        info=models.Sys_File(FileId=tool.guid(),FileName="big",
                            Extension="txt",FileSize=len(all_the_text),
                            UpTime=datetime.datetime.now(),ModelId=ModelId)
    
        info.FilePath=__GetPath__(info.FileId)
        file_object = open(info.FilePath, 'w')
        file_object.writelines(all_the_text)
        file_object.close()
        return info
    
#获取文件
def GetTxt(ModelId):
        info=models.Sys_File.objects.get(FileId=ModelId)
        return open(info.FilePath, "rb").read()
    
def Show(request):
        info=models.Sys_File.objects.get(FileId=request.GET.get('Id',''))
        image_data = open(info.FilePath, "rb").read()
        return HttpResponse(image_data, mimetype="image")

def Down(request):
        info=models.Sys_File.objects.get(FileId=request.GET.get('Id',''))
        image_data = open(info.FilePath, "rb").read()
        return HttpResponse(image_data)
    
    
    
    
    
    